﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SmartMobile.Data;
using SMARTMobile.Models;
using SMARTMobile.WinPhone;

namespace SMARTMobile.ViewModels
{
    public class PhonevendorVolumeViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<PhonevenforVolumeExtend> _phonevendorVolumeObservable; 

        #endregion

        public PhonevendorVolumeViewModel()
        {
            this.CreateConnection();
            this.GetPhonevendorVolumen();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<PhonevenforVolumeExtend> PhonevendorVolumeObservable
        {
            get { return this._phonevendorVolumeObservable; }
            set
            {
                this._phonevendorVolumeObservable = value;
                base.RaisePropertyChanged("PhonevendorVolumeObservable");
            }
        }

        #endregion

        #region Command

        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }

        #endregion

        #region Command Action Handlers

        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.PhonevendorVolume.DeleteAllOnSubmit(
                    this._smartMobileContext.PhonevendorVolume.Where(
                        x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());

                this._smartMobileContext.SubmitChanges();
            }

            List<PhonevendorVolume> phonevendorVolumeList = new List<PhonevendorVolume>();
            foreach (var phonevendorVolume in this.PhonevendorVolumeObservable)
            {
                PhonevendorVolume vender = new PhonevendorVolume
                {
                    ID_PK = phonevendorVolume.ID_PK,
                    CustomerKey = phonevendorVolume.CustomerKey,
                    PhoneVendorCode = phonevendorVolume.PhoneVendorCode,
                    RetailValue= phonevendorVolume.RetailValue,
                    RetailVolume = phonevendorVolume.RetailVolume,
                    WhosesaleValue = phonevendorVolume.WhosesaleValue,
                    WhosesaleVolume = phonevendorVolume.WhosesaleVolume
                };
                phonevendorVolumeList.Add(vender);
            }
            this._smartMobileContext.PhonevendorVolume.InsertAllOnSubmit(phonevendorVolumeList);
            this._smartMobileContext.SubmitChanges();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }

        #endregion

        #region Method

        private void GetPhonevendorVolumen()
        {
            List<PhoneVendor> phoneVenders = this._smartMobileContext.PhoneVendor.ToList();
            if (phoneVenders.Count == 0)
            {
                return;
            }

            this.PhonevendorVolumeObservable = new ObservableCollection<PhonevenforVolumeExtend>();
            foreach (var item in phoneVenders)
            {
                PhonevenforVolumeExtend volume = new PhonevenforVolumeExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    PhoneVendorName = item.PhoneVendorName,
                    RetailVolume = 0,
                    RetailValue = 0,
                    WhosesaleValue = 0,
                    WhosesaleVolume = 0
                };

                this.PhonevendorVolumeObservable.Add(volume);                
            }

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<PhonevendorVolume> phonevendorVolumes = this._smartMobileContext.PhonevendorVolume.Where(p => p.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (phonevendorVolumes.Count > 0)
                {
                    if (this.PhonevendorVolumeObservable.Count == phonevendorVolumes.Count)
                    {
                        this.PhonevendorVolumeObservable = new ObservableCollection<PhonevenforVolumeExtend>();
                        foreach (var item in phonevendorVolumes)
                        {
                            PhonevenforVolumeExtend volume = new PhonevenforVolumeExtend
                            {
                                ID_PK = item.ID_PK,
                                CustomerKey = item.CustomerKey,
                                PhoneVendorCode = item.PhoneVendorCode,
                                PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode),
                                RetailVolume = item.RetailVolume,
                                RetailValue = item.RetailValue,
                                WhosesaleValue = item.WhosesaleValue,
                                WhosesaleVolume = item.WhosesaleVolume
                            };

                            this.PhonevendorVolumeObservable.Add(volume);
                        }
                    }
                    else
                    {
                        foreach (var item in phonevendorVolumes)
                        {
                            PhonevenforVolumeExtend selectedItem = this.PhonevendorVolumeObservable.FirstOrDefault(p => (p.CustomerKey == item.CustomerKey && p.PhoneVendorCode == item.PhoneVendorCode));
                            if (selectedItem != null)
                            {
                                selectedItem.ID_PK = item.ID_PK;
                                selectedItem.CustomerKey = item.CustomerKey;
                                selectedItem.PhoneVendorCode = item.PhoneVendorCode;
                                selectedItem.PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode);
                                selectedItem.RetailVolume = item.RetailVolume;
                                selectedItem.RetailValue = item.RetailValue;
                                selectedItem.WhosesaleValue = item.WhosesaleValue;
                                selectedItem.WhosesaleVolume = item.WhosesaleVolume;
                            }                            
                        }

                        this._smartMobileContext.PhonevendorVolume.DeleteAllOnSubmit(this._smartMobileContext.PhonevendorVolume);
                        this._smartMobileContext.SubmitChanges();

                        List<PhonevendorVolume> phonevendorVolumeList = new List<PhonevendorVolume>();
                        foreach (var phonevendorVolume in this.PhonevendorVolumeObservable)
                        {
                            PhonevendorVolume vender = new PhonevendorVolume
                            {
                                ID_PK = phonevendorVolume.ID_PK,
                                CustomerKey = phonevendorVolume.CustomerKey,
                                PhoneVendorCode = phonevendorVolume.PhoneVendorCode,
                                RetailValue = phonevendorVolume.RetailValue,
                                RetailVolume = phonevendorVolume.RetailVolume,
                                WhosesaleValue = phonevendorVolume.WhosesaleValue,
                                WhosesaleVolume = phonevendorVolume.WhosesaleVolume
                            };
                            phonevendorVolumeList.Add(vender);
                        }
                        this._smartMobileContext.PhonevendorVolume.InsertAllOnSubmit(phonevendorVolumeList);
                        this._smartMobileContext.SubmitChanges();
                    }                    
                }
                else
                {
                    List<PhonevendorVolume> phonevendorVolumeList = new List<PhonevendorVolume>();
                    foreach (var phonevendorVolume in this.PhonevendorVolumeObservable)
                    {
                        PhonevendorVolume vender = new PhonevendorVolume
                        {
                            ID_PK = phonevendorVolume.ID_PK,
                            CustomerKey = phonevendorVolume.CustomerKey,
                            PhoneVendorCode = phonevendorVolume.PhoneVendorCode,
                            RetailValue = phonevendorVolume.RetailValue,
                            RetailVolume = phonevendorVolume.RetailVolume,
                            WhosesaleValue = phonevendorVolume.WhosesaleValue,
                            WhosesaleVolume = phonevendorVolume.WhosesaleVolume
                        };
                        phonevendorVolumeList.Add(vender);
                    }
                    this._smartMobileContext.PhonevendorVolume.InsertAllOnSubmit(phonevendorVolumeList);
                    this._smartMobileContext.SubmitChanges();
                }
            }
        }

        private string GetPhoneVendorName(string phonevendorCode)
        {
            return this._smartMobileContext.PhoneVendor.Where(p => p.PhoneVendorCode == phonevendorCode).Select(p => p.PhoneVendorName).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        #endregion
    }
}